package ServerSide;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.*;

/**
 *
 * @author user
 */
public class database {

    public static String dbName = "dbServerProgin5";
    public static String dbUsername = "progin";
    public static String dbPassword = "progin";
    public static String dbHost = "localhost";

    private static Connection dbConnect(String db_connect_string, String db_userid, String db_password) {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static Connection connectDB() {
        return database.dbConnect("jdbc:mysql://" + dbHost + ":3306/" + dbName, dbUsername, dbPassword);
    }

    private static ResultSet GetUserRow(String username) throws SQLException{
        String query = "SELECT * FROM user WHERE username = '" + username + "'";
        Statement statement = connectDB().createStatement();
        ResultSet resultset = statement.executeQuery(query);
        return resultset;
    }

    public static boolean CheckUser(String username, String password) throws SQLException{
        ResultSet resultSet = GetUserRow(username);
        resultSet.next();
        return (resultSet.getString("password").equals(password));
    }

    public static int GetHeadRevision(String username) throws SQLException{
        ResultSet resultSet = GetUserRow(username);
        resultSet.next();
        return resultSet.getInt("headRevision");
    }

    public static void IncreaseRevision(String username) throws SQLException{
        String query = "UPDATE user SET headRevision = headRevision+1 WHERE username = '"+username+"'";
        Statement statement = connectDB().createStatement();
        statement.executeUpdate(query);
    }
}
